/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package heartsinjava2;

import javax.swing.ImageIcon;

/**
 *
 * @author drsagitn
 */
public final class Card implements Comparable<Card> {

        private  final Suit suit;
        private  final Rank rank;
        private ImageIcon cardImage;
        private static boolean sortRankMajorOrder = true;

        public Card(Suit suit, Rank rank) {
            this.suit = suit;
            this.rank = rank;
        }

       public Card(Suit Suit, Rank Rank, ImageIcon cardFace) {
          cardImage = cardFace;
          suit = Suit;
          rank = Rank;
        }


        public final Suit getSuit() {
            return this.suit;
        }

        public final Rank getRank() {
            return this.rank;
        }

         public ImageIcon getCardImage() {
             return cardImage;
        }

        public static void setRankMajorSort() {
         sortRankMajorOrder = true;
        }

        public static void setSuitMajorSort() {
            sortRankMajorOrder = false;
         }

        public static String getFilename( Suit suit, Rank rank ) {
            switch(rank){
                case ACE:
                     return "a" + suit.toString() + ".gif";
                case DEUCE:
                     return "2" + suit.toString() + ".gif";
                case THREE:
                     return "3" + suit.toString() + ".gif";
                case FOUR:
                     return "4" + suit.toString() + ".gif";
                case FIVE:
                     return "5" + suit.toString() + ".gif";
                case SIX:
                     return "6" + suit.toString() + ".gif";
                case SEVEN:
                     return "7" + suit.toString() + ".gif";
                case EIGHT:
                     return "8" + suit.toString() + ".gif";
                case NINE:
                     return "9" + suit.toString() + ".gif";
                case TEN:
                     return "t" + suit.toString() + ".gif";
                case JACK:
                     return "j" + suit.toString() + ".gif";
                case QUEEN:
                     return "q" + suit.toString() + ".gif";
                case KING:
                     return "k" + suit.toString() + ".gif";
            }

          return "null";
       }



        @Override
        public String toString() {
            return String.format("%s of %s", this.rank, this.suit);
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }

            if (getClass() != obj.getClass()) {
                return false;
            }

            final Card other = (Card) obj;

            if (this.suit != other.suit) {
                return false;
            }

            if (this.rank != other.rank) {
                return false;
            }

            return true;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 61 * hash + (this.suit != null ? this.suit.hashCode() : 0);
            hash = 61 * hash + (this.rank != null ? this.rank.hashCode() : 0);
            return hash;
        }

        // compares two cards(int): if suits are the same, compares ranks.
        public int compareTo(Card card) {

            if (this.suit.compareTo(card.getSuit()) == 0) {
                return this.rank.compareTo(card.getRank());
            } else {
                return this.suit.compareTo(card.getSuit());
            }
        }
    }